<template>
	<view class="topbox">
		<view class="cont">
			<view class="dzbox" @click="selectsite">
				<view class="leftbox" v-if="show">
					<view class="namebox">
						<text class="name">{{siteobj.name}}</text>
						<text>{{siteobj.phone}}</text>
						<text class="moren" v-if="siteobj.is_default==1">默认</text>
					</view>
					<view class="site">
						{{siteobj.address}}
					</view>
				</view>
				<view class="" v-else>
					快去设置一个地址吧
				</view>
				<view class="angle"></view>
			</view>
			<view class="hrs"></view>
			<view class="boxs" v-for="(item,index) in list" :key="index">
				<view class="spxx">
					<image :src="item.attr.img?item.attr.img:item.commodity.img" mode=""></image>
					<view class="rightbox">
						<view class="namebox">
							<view class="name">
								{{item.attr.name}}
							</view>
							<view class="shulian">
								×{{item.num}}
							</view>
						</view>
						<view class="centerbox">
							{{item.attr.commodity_attr_ids}}
						</view>
						<!-- <view class="jifenbox">
							<text class="num">200</text>
							<text>积分</text>
						</view> -->
						<view class="jiegebox">
							<view class="ltbox">
								<text>￥</text>
								<text class="qian">{{item.attr.prices}}</text>
							</view>
							<view class="rgtbox">
								<text>总价：</text>
								<text class="qian">{{(item.attr.prices*item.num).toFixed(2)}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="hr"></view>
			</view>
			<view class="hrs"></view>
			<view class="yhqbox" @click="mycoupon">
				<text class="yh">优惠券</text>
				<view class="rightbox">
					<text>{{obj.coupons.length}}张可用</text>
					<view class="angle"></view>
				</view>
			</view>
			<view class="yhqbox" @click="mycoupon">
				<text class="yh">商品合计</text>
				<view class="rightbox">
					<text>￥{{total}}</text>
				</view>
			</view>
			<view class="yhqbox">
				<text class="yh">金币抵扣</text>
				<view class="rightbox">
					<text>使用1金币可抵扣¥1.00现金</text>
					<u-switch v-model="checked" active-color="#CC3333" size="40" @change="switchbtn"></u-switch>
				</view>
			</view>
			<view class="hrs"></view>
			<!-- <view class="heji">
				<text class="hjname"> 运费</text>
				<text>￥0.00</text>
			</view> -->
			<view class="heji">
				<text class="hjname">优惠券</text>
				<text>-￥{{xzcoupons.money?xzcoupons.money:'0.00'}}</text>
			</view>
			<view class="heji">
				<text class="hjname"> 金币抵扣</text>
				<text>-￥{{gold_num}}</text>
			</view>
			<view class="heji">
				<text class="hjname"> 订单备注</text>
				<textarea value="" placeholder="选填,请输入买家备注" v-model="remarks" />
			</view>
			<view class="" style="height: 250rpx;">

			</view>
		</view>
		<view class="tijiaobox">
			<view class="fukuan">
				还需支付：<text>{{totals}}</text>
			</view>
			<view class="tijiao" @click="pay">
				提交订单
			</view>
		</view>
	</view>
</template>

<script>
	import {
		settlementsubmit,
		submitorder
	} from "../../api/api/index.js"
	export default {
		data() {
			return {
				jkarr: [],
				siteobj: {},
				list: [],
				identity: 0,
				obj: {},
				xzcoupons: {},
				show: true,
				allprice: "",
				remarks: "",
				// 总价
				total: 0,
				// 当有优惠券时候
				totals: 0,
				checked: false,
				// 是否使用金币
				gold: 0,
				gold_num: 0,
				max_gold: 0
			}
		},
		methods: {
			cope(v) {
				let a = v.toString()
				uni.setClipboardData({
					data: a,
					success: function() {},
					fail() {
						console.log("失败")
					}
				});
			},
			// 选择地址
			selectsite() {
				this.jumpLink({
					link: "/pages/selectsite/index"
				})
			},
			// 优惠券
			mycoupon() {
				this.jumpLink({
					link: "/pages/my_coupon/index"
				})
			},
			// 支付
			pay() {
				let dataobj = []
				for (let i = 0; i < this.obj.data.length; i++) {
					dataobj.push({
						commodity_id: this.obj.data[i].commodity_id,
						num_id: this.obj.data[i].num_id,
						num: this.obj.data[i].num
					})
				}
				let shuju = {
					data: dataobj,
					address_id: this.siteobj.id,
					coupons_id: this.xzcoupons.coupons_id,
					type: "wechat_applet",
					remarks: this.remarks,
					gold:this.gold
				}
				submitorder(shuju).then(res => {
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.data.timestamp,
						nonceStr: res.data.noncestr,
						package: res.data.package,
						signType: 'MD5',
						paySign: res.data.sign,
						success: function(res) {
							console.log('success: ' + JSON.stringify(res));
							this.jumpLink({
								link: "/pages/payment_success/index"
							})
						},
						fail: function(err) {
							console.log('fail:' + JSON.stringify(err));
						}
					});
				})
			},
			// 计算总价
			statistics() {
				let total = 0
				this.list.forEach((item, index) => {
					total = total + item.attr.prices * item.num
				})
				this.total = total
				console.log(this.total)
				if (this.xzcoupons != "") {
					this.totals = total - this.xzcoupons.money - this.gold_num * 1
				} else {
					this.totals = total - this.gold_num * 1
				}

			},
			// 开关选择器
			switchbtn(e) {
				// console.log(e)
				// debugger
				if (e) {
					this.gold = 1
					this.gold_num = this.max_gold
				} else {
					this.gold = 0
					this.gold_num = 0
				}
				this.statistics()
			}
		},
		onLoad(v) {
			var arr = [v]
			if (v.id == 0) {
				let b = JSON.parse(v.list)
				this.jkarr = b
			}
			if (v.id == 1) {
				this.jkarr = arr
			}
			this.identity = uni.getStorageSync("identity")
			// this.xzcoupons = uni.getStorageSync("xzcoupons")
			this.siteobj = uni.getStorageSync("selectsite")
		},
		onShow() {
			this.xzcoupons = uni.getStorageSync("xzcoupons")
			let that = this
			settlementsubmit({
				data: this.jkarr
			}).then(res => {
				if (uni.getStorageSync("selectsite") != "") {
					this.siteobj = uni.getStorageSync("selectsite")
				} else {
					this.siteobj = res.data.address[0]
				}
				// 当开关选择器打开时显示抵扣多少
				this.max_gold = res.data.max_gold
				// 当没有地址时
				if (res.data.address.length == "") {
					this.show = false
					uni.showModal({
						title: "您还没有设置收货地址哦，赶快设置一个吧",
						success: function(res) {
							if (res.confirm) {
								that.jumpLink({
									link: "/pages/selectsite/index"
								})
							} else if (res.cancel) {
								uni.navigateBack({
									delta: 1
								})
							}
						}
					})
				}
				this.obj = res.data
				// 判断身份
				this.list = res.data.data.map(v => {
					if (this.identity == 0) {
						v.attr.prices = v.attr.price
						if (uni.getStorageSync("xzcoupons") != "") {
							this.allprice = v.attr.prices * v.num - this.xzcoupons.money
						} else {
							this.allprice = v.attr.prices * v.num
						}
					} else {
						v.attr.prices = v.attr.distributors_price
						if (uni.getStorageSync("xzcoupons") != "") {
							this.allprice = v.attr.distributors_price * v.num - this.xzcoupons.money
						} else {
							this.allprice = v.attr.distributors_price * v.num
						}
					}
					return v
				})
				this.statistics()
				uni.setStorageSync("coupons", res.data.coupons)
			})
		}
	}
</script>

<style scoped lang="scss">
	.topbox {
		background-color: #f42121;
		padding-top: 50rpx;
		box-sizing: border-box;
		position: fixed;
		top: 0;
		bottom: 0;
		right: 0;
		left: 0;

		.cont {
			position: fixed;
			background-color: #FFFFFF;
			top: 0;
			right: 25rpx;
			left: 25rpx;
			bottom: 0;
			height: 1406rpx;
			overflow: scroll;
		}

		.dzbox {
			background-color: #FFFFFF;
			// width: 700rpx;
			border-top-left-radius: 6rpx;
			border-top-right-radius: 6rpx;
			height: 166rpx;
			// margin-left: 25rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 45rpx 38rpx 36rpx 29rpx;
			box-sizing: border-box;
			margin-top: 13rpx;

			.namebox {
				display: flex;
				align-items: center;

				text {
					font-family: MicrosoftYaHei;
					font-size: 24rpx;
					color: #282828;
				}

				.name {
					font-family: MicrosoftYaHei-Bold;
					font-size: 28rpx;
					color: #333333;
					font-weight: 700;
					margin-right: 20rpx;
				}

				.moren {
					width: 57rpx;
					display: block;
					height: 26rpx;
					text-align: center;
					line-height: 26rpx;
					background-color: #FFE3AA;
					border-radius: 26rpx;
					font-size: 18rpx;
					font-family: Microsoft YaHei;
					color: #CC6600;
					margin-left: 20rpx;
				}
			}

			.site {
				font-family: MicrosoftYaHei;
				font-size: 24rpx;
				color: #282828;
				margin-top: 30rpx;
			}
		}

		.angle {
			width: 20rpx;
			height: 20rpx;
			border-top: 1px solid #000;
			border-left: 1px solid #000;
			transform: rotate(135deg)
		}
	}

	.boxs {
		background-color: #FFFFFF;
		padding-left: 10rpx;
		padding-right: 10rpx;
		padding-top: 32rpx;
		box-sizing: border-box;
		// width: 700rpx;
		border-bottom-left-radius: 6rpx;
		border-bottom-right-radius: 6rpx;

		.hr {
			width: 678rpx;
			height: 1rpx;
			background-color: #000000;
			opacity: 0.2;
			margin-top: 30rpx;
			margin-bottom: 40rpx;
		}

		.spxx {
			margin-left: 19rpx;
			display: flex;
			margin-top: 30rpx;
			margin-bottom: 30rpx;

			image {
				width: 181rpx;
				height: 181rpx;
			}

			.rightbox {
				margin-left: 18rpx;

				.namebox {
					display: flex;
					justify-content: space-between;

					.name {
						width: 397rpx;
						margin-right: 30rpx;
						line-height: 36rpx;
						font-family: MicrosoftYaHei;
						font-size: 24rpx;
						color: #000000;
					}

					.shulian {
						font-family: MicrosoftYaHei;
						font-size: 30rpx;
						color: #000000;
					}
				}

				.jifenbox {
					font-family: MicrosoftYaHei;
					font-size: 20rpx;
					color: #666666;
					margin-top: 19rpx;

					.num {
						font-family: PingFang-SC-Medium;
						font-size: 30rpx;
						color: #ff3333;
						margin-right: 20rpx;
					}
				}

				.jiegebox {
					display: flex;
					justify-content: space-between;
					margin-top: 35rpx;

					.ltbox {
						font-family: PingFang-SC-Medium;
						font-size: 18rpx;
						color: #ff3333;

						.qian {
							font-family: PingFang-SC-Medium;
							font-size: 30rpx;
							color: #ff3333;
						}
					}

					.rgtbox {
						font-family: PingFang-SC-Regular;
						font-size: 20rpx;
						color: #666666;
						margin-top: 20rpx;

						.qian {
							font-family: PingFang-SC-Medium;
							font-size: 30rpx;
							color: #ff3333;
						}
					}

				}

				.centerbox {
					font-family: MicrosoftYaHei;
					font-size: 20rpx;
					color: #666666;
					margin-top: 15rpx;
				}
			}
		}

		.smallbox {
			height: 100rpx;
		}
	}

	.hrs {
		height: 15rpx;
		background-color: #ededed;
	}

	.heji {
		display: flex;
		justify-content: space-between;
		font-family: PingFang-SC-Medium;
		font-size: 24rpx;
		color: #666666;
		margin-top: 50rpx;
		margin-left: 21rpx;
		margin-right: 21rpx;

		.hjname {
			font-family: MicrosoftYaHei;
			font-size: 24rpx;
			color: #000000;
			width: 120rpx;
		}

		textarea {
			margin-top: 6rpx;
			margin-left: 5rpx;
			height: 200rpx;
		}
	}

	.tijiaobox {
		display: flex;
		justify-content: space-between;
		// position: ;
		position: absolute;
		bottom: 0;
		right: 0;
		left: 0;
		background-color: #FFFFFF;
		height: 100rpx;
		line-height: 100rpx;

		.fukuan {
			font-family: MicrosoftYaHei;
			font-size: 22rpx;
			color: #8b8b8b;
			margin-left: 45rpx;

			text {
				font-family: PingFang-SC-Bold;
				font-size: 32rpx;
				color: #e40000;
			}
		}

		.tijiao {
			width: 210rpx;
			height: 78rpx;
			background-color: #ff8400;
			border-radius: 39rpx;
			line-height: 78rpx;
			font-family: MicrosoftYaHei;
			font-size: 24rpx;
			color: #FFFFFF;
			text-align: center;
			margin-top: 15rpx;
			margin-right: 20rpx;
		}
	}

	.yhqbox {
		display: flex;
		justify-content: space-between;
		height: 100rpx;
		line-height: 100rpx;

		.yh {
			font-family: MicrosoftYaHei;
			font-size: 24rpx;
			color: #000000;
			margin-left: 29rpx;
		}

		.rightbox {
			display: flex;
			align-items: center;
			margin-right: 20rpx;

			text {
				font-size: 22rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}
		}
	}
</style>
